package com.neusoft.yuejian12.yuejian_12.activity;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.neusoft.yuejian12.yuejian_12.R;
import com.neusoft.yuejian12.yuejian_12.fragment.ClubFragment;
import com.neusoft.yuejian12.yuejian_12.fragment.MyFragment;
import com.neusoft.yuejian12.yuejian_12.fragment.PostFragment;
import com.neusoft.yuejian12.yuejian_12.fragment.ShouyeFragment;
import com.neusoft.yuejian12.yuejian_12.fragment.TrainFragment;


public class MainActivity extends FragmentActivity implements View.OnClickListener
{
    private LinearLayout mTabTrain;
    private LinearLayout mTabClub;
    private LinearLayout mTabShouye;
    private LinearLayout mTabPost;
    private LinearLayout mTabMy;

    private ImageButton mImgTrain;
    private ImageButton mImgClub;
    private ImageButton mImgShouye;
    private ImageButton mImgPost;
    private ImageButton mImgMy;

    private TextView mTextTrain;
    private TextView mTextClub;
    private TextView mTextShouye;
    private TextView mTextPost;
    private TextView mTextMy;


    private Fragment mTab01;
    private Fragment mTab02;
    private Fragment mTab03;
    private Fragment mTab04;
    private Fragment mTab05;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();//初始化页面
        initEvent();//初始化事件
        setSelect(0);
    }

    private void initEvent()
    {
        mTabTrain.setOnClickListener(this);
        mTabClub.setOnClickListener(this);
        mTabShouye.setOnClickListener(this);
        mTabPost.setOnClickListener(this);
        mTabMy.setOnClickListener(this);
    }

    private void initView()
    {
        mTabTrain = (LinearLayout) findViewById(R.id.id_tab_train);
        mTabClub = (LinearLayout) findViewById(R.id.id_tab_club);
        mTabShouye = (LinearLayout) findViewById(R.id.id_tab_shouye);
        mTabPost = (LinearLayout) findViewById(R.id.id_tab_post);
        mTabMy = (LinearLayout) findViewById(R.id.id_tab_my);

        mImgTrain = (ImageButton) findViewById(R.id.id_tab_train_img);
        mImgClub = (ImageButton) findViewById(R.id.id_tab_club_img);
        mImgShouye = (ImageButton) findViewById(R.id.id_tab_shouye_img);
        mImgPost = (ImageButton) findViewById(R.id.id_tab_post_img);
        mImgMy = (ImageButton) findViewById(R.id.id_tab_my_img);

        mTextTrain = (TextView) findViewById(R.id.id_tab_train_text);
        mTextClub = (TextView) findViewById(R.id.id_tab_club_text);
        mTextShouye = (TextView) findViewById(R.id.id_tab_shouye_text);
        mTextPost = (TextView) findViewById(R.id.id_tab_post_text);
        mTextMy = (TextView) findViewById(R.id.id_tab_my_text);
    }

    private void setSelect(int i)
    {

        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        switch (i)
        {
            case 0:
                if (mTab01 == null)
                {
                    mTab01 = new TrainFragment();
                    transaction.add(R.id.id_content, mTab01);
                } else
                {
                    transaction.show(mTab01);
                }
                mImgTrain.setImageResource(R.drawable.ic_train_red);
                mTextTrain.setTextColor(Color.rgb(255,69,0));
                break;
            case 1:
                if (mTab02 == null)
                {
                    mTab02 = new ClubFragment();
                    transaction.add(R.id.id_content, mTab02);
                } else
                {
                    transaction.show(mTab02);
                }
                mImgClub.setImageResource(R.drawable.ic_club_red);
                mTextClub.setTextColor(Color.rgb(255,69,0));
                break;
            case 2:
                if (mTab03 == null)
                {
                    mTab03 = new ShouyeFragment();
                    transaction.add(R.id.id_content, mTab03);
                } else
                {
                    transaction.show(mTab03);
                }
                mImgShouye.setImageResource(R.drawable.ic_shouye_red);
                mTextShouye.setTextColor(Color.rgb(255,69,0));
                break;
            case 3:
                if (mTab04 == null)
                {
                    mTab04 = new PostFragment();
                    transaction.add(R.id.id_content, mTab04);
                } else
                {
                    transaction.show(mTab04);
                }
                mImgPost.setImageResource(R.drawable.ic_post_red);
                mTextPost.setTextColor(Color.rgb(255,69,0));
                break;
            case 4:
                if (mTab05 == null)
                {
                    mTab05 = new MyFragment();
                    transaction.add(R.id.id_content, mTab05);
                } else
                {
                    transaction.show(mTab05);
                }
                mImgMy.setImageResource(R.drawable.ic_my_red);
                mTextMy.setTextColor(Color.rgb(255,69,0));
                break;
            default:
                break;
        }

        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction)
    {
        if (mTab01 != null)
        {
            transaction.hide(mTab01);
        }
        if (mTab02 != null)
        {
            transaction.hide(mTab02);
        }
        if (mTab03 != null)
        {
            transaction.hide(mTab03);
        }
        if (mTab04 != null)
        {
            transaction.hide(mTab04);
        }
        if (mTab05 != null)
        {
            transaction.hide(mTab05);
        }
    }

    @Override
    public void onClick(View v)
    {
        resetImgs();
        resetText();
        switch (v.getId())
        {
            case R.id.id_tab_train:
                setSelect(0);
                break;
            case R.id.id_tab_club:
                setSelect(1);
                break;
            case R.id.id_tab_shouye:
                setSelect(2);
                break;
            case R.id.id_tab_post:
                setSelect(3);
                break;
            case R.id.id_tab_my:
                setSelect(4);
                break;
            default:
                break;
        }
    }

    private void resetImgs()
    {
        mImgTrain.setImageResource(R.drawable.ic_train_hui);
        mImgClub.setImageResource(R.drawable.ic_club_hui);
        mImgShouye.setImageResource(R.drawable.ic_shouye_hui);
        mImgPost.setImageResource(R.drawable.ic_post_hui);
        mImgMy.setImageResource(R.drawable.ic_my_hui);
    }
    private void resetText()
    {
        mTextTrain.setTextColor(Color.rgb(0,0,0));
        mTextClub.setTextColor(Color.rgb(0,0,0));
        mTextShouye.setTextColor(Color.rgb(0,0,0));
        mTextPost.setTextColor(Color.rgb(0,0,0));
        mTextMy.setTextColor(Color.rgb(0,0,0));
    }

}